package cn.lintcode;

/**
 * @author labvi
 * @version 1.0.0
 * 167 两个链表求和
 */
public class AddTwoNum {
    public static class ListNode {
        int val;
        ListNode next;
        ListNode(int x) {
            val = x;
            next = null;
        }
    }
    public ListNode addLists(ListNode l1, ListNode l2) {
        // write your code here
        ListNode result = new ListNode(0);
        ListNode last = result;
        int add = 0;
        while(l1 != null && l2 != null){
            int a = l1.val + l2.val;
            last.next = new ListNode(a % 10 + add);
            add = a / 10;
            l1 = l1.next;
            l2 = l2.next;
            last = last.next;
        }

        return result.next;
    }

    public static void main(String[] args) {
        ListNode node1 = new ListNode(9);
        ListNode node2 = new ListNode(9);
        ListNode node3 = new ListNode(9);
        node1.next = node2;
        AddTwoNum addTwoNum = new AddTwoNum();
        addTwoNum.addLists(node1,node3);
    }
}
